package stream;

import reference.Student;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author LiuYan
 * @date 2021/11/19 9:37
 * 查找与匹配
 * allMatch-检查是否匹配所有元素
 * anyMatch-检查是否至少匹配一个元素
 * noneMatch-检查是否没有匹配所有元素
 * findAny-返回当前流中的任意元素
 * count-返回流中元素的总个数
 * max-返回流中最大值
 * min-返回流中最小值
 */
public class Part09 {
    public static void main(String[] args) {
        List<Student> studentList = Arrays.asList(
                new Student(20, "lisi"),
                new Student(10, "zhangsan"),
                new Student(30, "wangwu"),
                new Student(20, "lisi1"),
                new Student(10, "zhangsan1"),
                new Student(30, "wangwu1"),
                new Student(20, "lisi2"),
                new Student(10, "zhangsan2"),
                new Student(30, "wangwu2"),
                new Student(40, "goudan"),
                new Student(20, "lisi3"),
                new Student(10, "zhangsan4"),
                new Student(30, "wangwu5"),
                new Student(20, "lisi6"),
                new Student(10, "zhangsan7"),
                new Student(30, "wangwu8"),
                new Student(20, "lisi9"),
                new Student(10, "zhangsan22"),
                new Student(30, "wangwu23"),
                new Student(40, "goudan333"));
        boolean match = studentList.stream().allMatch(s -> s.getAge() >= 10);
        System.out.println(match);

        boolean anyMatch = studentList.stream().anyMatch(s -> s.getAge() >= 40);
        System.out.println(anyMatch);

        Optional<Student> first = studentList.stream().findFirst();
        System.out.println(first.orElse(new Student()));
        // 并行流随机抢夺一个元素
        Optional<Student> any = studentList.parallelStream().findAny();
        System.out.println(any.get());

        long count = studentList.stream().count();
        System.out.println(count);
        Optional<Student> max = studentList.stream().max((o1, o2) -> o1.getAge().compareTo(o2.getAge()));
        System.out.println(max.get());

        Optional<Student> min = studentList.stream().min((o1, o2) -> o1.getAge().compareTo(o2.getAge()));
        System.out.println(min.get());




    }
}
